package com.xbongbong.sys.help;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.pro.domain.dao.CloudCodePermissionDao;
import com.xbongbong.pro.domain.entity.CloudCodePermissionEntity;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.sys.pojo.DynamicPermissionPojo;
import com.xbongbong.sys.pojo.DynamicPermissionSubPojo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2021/10/20 15:59
 */
@Component
public class SysCloudCodeHelp {

    @Resource
    private CloudCodePermissionDao cloudCodePermissionDao;

    public Set<String> getCompanyDynamicPermissionSet(String corpid) {
        Set<String> dynamicPermissionSet = new HashSet<>();

        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("enable", BasicConstant.ONE);
        param.put("del", 0);
        List<CloudCodePermissionEntity> cloudCodePermissions = cloudCodePermissionDao.findEntities(param);
        for (CloudCodePermissionEntity cloudCodePermission : cloudCodePermissions) {
            dynamicPermissionSet.add(cloudCodePermission.getAlias());
        }
        return dynamicPermissionSet;
    }

    /**
     * 获取动态权限的alias集合
     * @param dynamicPermissions
     * @return
     * 创建时间 2020/9/27 5:18 PM
     * 修改时间 2020/9/27 5:18 PM
     * @author chy
     */
    public static Set<String> getDynamicPermissionSet(String dynamicPermissions) {
        Set<String> dynamicPermissionSet = new HashSet<>();
        List<DynamicPermissionPojo> dynamicPermissionPojos = JSON.parseArray(dynamicPermissions, DynamicPermissionPojo.class);
        if (Objects.isNull(dynamicPermissionPojos) || dynamicPermissionPojos.isEmpty()) {
            return dynamicPermissionSet;
        }
        for (DynamicPermissionPojo dynamicPermissionPojo : dynamicPermissionPojos) {
            List<DynamicPermissionSubPojo> modules = dynamicPermissionPojo.getModules();
            for (DynamicPermissionSubPojo module : modules) {
                dynamicPermissionSet.add(module.getAlias());
            }
        }
        return dynamicPermissionSet;
    }

}
